--renderer for game objects like players and such
local priority = 4

EntitySystem.rendersystem("drawObject", priority, function()
    local cx,cy = camera:pos()
    
  -- draw box
  for e in pairs(EntitySystem.query("simpleObject")) do
    love.graphics.setColor(e.color.r, e.color.g, e.color.b, e.color.a)
    
    if e.shape.form == "rect" then
      love.graphics.rectangle("fill", e.position.x, e.position.y, e.shape.size.x, e.shape.size.y)
    elseif e.shape.form == "circ" then
      love.graphics.circle("fill", e.position.x, e.position.y, e.shape.size.x/2, e.shape.size.y/2)
    end
  end
  
  love.graphics.setColor(color.white)
  --Static Objects
  for e in pairs(EntitySystem.query("texturedObject")) do
    if e.texture ~= nil then
      local offset = {x = e.texture.image:getWidth()/2, y = e.texture.image:getHeight()/2}
      local d = math.abs(math.dist(player.position.x, player.position.y,e.position.x, e.position.y))
      
      if d < 2500 then
      if e.physics ~= nil and e.physics.body ~= nil then    
          local rotation = e.physics.body:getAngle()
          love.graphics.draw(e.texture.image, e.physics.body:getX(), e.physics.body:getY(), rotation, 1, 1, offset.x,offset.y)
      else
        love.graphics.draw(e.texture.image, e.position.x, e.position.y, 0, 1, 1, offset.x,offset.y)
      end
      end
    end
  end
  
  --Animated Objects
  for e in pairs(EntitySystem.query("animatedObject")) do
    love.graphics.setColor(color.white)
    local offset = {x = e.animation.image:getWidth()/e.animation.numFrames/2, y = e.animation.image:getHeight()/2}
    local d = math.abs(math.dist(player.position.x, player.position.y,e.position.x, e.position.y))
    
    if d < 2500 then
    if e.physics ~= nil then
      local rotation
      local image
      if e.movement then
        if e.movement.turbo then
          image = e.animation.turboImage
        else
          image = e.animation.image
        end
        else
          image = e.animation.image
      end
      if e.physics.body then
        e.animation.anim:draw(image, e.physics.body:getX(), e.physics.body:getY(), e.physics.body:getAngle(), 1, 1, offset.x,offset.y)
      else
        e.animation.anim:draw(image, e.position.x, e.position.y, 0, 1, 1, offset.x,offset.y)
      end
      
      
    else
      e.animation.anim:draw(e.animation.image, e.position.x, e.position.y, 0, 1, 1, offset.x,offset.y)
    end
    end
  end
end)